﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows;

using Omniscient.TimeTracker.Data;
using Omniscient.Foundation.ApplicationModel;
using Omniscient.TimeTracker.BusinessModel;

namespace Omniscient.TimeTracker
{
    public class NotifierTray: IDisposable

    {
        private ITimeEntryManager _manager;
        private NotifyIcon _notifyIcon;
        private Window _parentForm;

        public NotifierTray(Window parentForm)
        {
            _parentForm = parentForm;
            _notifyIcon = new NotifyIcon();
            _manager = ApplicationManager.Current.ObjectContainer.Get<ITimeEntryManager>();
            _manager.StateChanged +=new StateChangedEventHandler(_manager_StateChanged);

            _notifyIcon.MouseDoubleClick += new MouseEventHandler(_notifyIcon_MouseDoubleClick);
            _notifyIcon.MouseUp += new MouseEventHandler(_notifyIcon_MouseUp);
            
            _notifyIcon.Icon = new Icon(this.GetType().Assembly.GetManifestResourceStream("Omniscient.TimeTracker.res.IconStop.ico"));

            _notifyIcon.Visible = true;
        }

    
        void _manager_StateChanged(object sender, EventArgs e)
        {
            if(_manager.HasRunningTask)
                _notifyIcon.Icon = new Icon(this.GetType().Assembly.GetManifestResourceStream("Omniscient.TimeTracker.res.IconStart.ico"));
            else
                _notifyIcon.Icon = new Icon(this.GetType().Assembly.GetManifestResourceStream("Omniscient.TimeTracker.res.IconStop.ico"));
            
            ShowTrayBallonTip(); 
        }
        

        void _notifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
        {
            if (_parentForm.WindowState == WindowState.Normal || _parentForm.WindowState == WindowState.Maximized)
                _parentForm.WindowState = WindowState.Minimized;
            else
                _parentForm.WindowState = WindowState.Normal;

            _parentForm.Topmost = true;
        }

        void _notifyIcon_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                NotifierContextMenu nm;
                nm = new NotifierContextMenu();
                nm.StaysOpen = false;
                nm.Placement = System.Windows.Controls.Primitives.PlacementMode.Mouse;
                nm.IsOpen = true;
            }
            else if (e.Button == MouseButtons.Left)
            {
                ShowTrayBallonTip();
            }
        }

        private void ShowTrayBallonTip()
        {
            if (_manager.HasRunningTask)
            {
                _notifyIcon.BalloonTipTitle = _manager.CurrentRunningTimeEntry.Task.ParentProject.Name;
                if (_manager.CurrentRunningTimeEntry.Category == null)
                    _notifyIcon.BalloonTipText = String.Format("Runing task : {0} Category : {1}", _manager.CurrentRunningTimeEntry.Task.Name, "None");
                else
                    _notifyIcon.BalloonTipText = String.Format("Runing task : {0} Category : {1}", _manager.CurrentRunningTimeEntry.Task.Name, _manager.CurrentRunningTimeEntry.Category.Name);
            }
            else
            {
                _notifyIcon.BalloonTipText = "No current running task.";
            }

            _notifyIcon.ShowBalloonTip(2000);
        }

        #region IDisposable Members

        public void Dispose()
        {
            _notifyIcon.Visible = false;
            _notifyIcon.Dispose();
        }
        #endregion
    }
}
